package netty.demo.nio.fileChannel;

import lombok.SneakyThrows;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @author Zed
 * @description
 * @date 2022/10/22 17:18
 */
public class testFileChannelTransferTo {
    @SneakyThrows
    public static void main(String[] args) {
        FileChannel from = new FileInputStream("data.txt").getChannel();
        FileChannel to = new FileOutputStream("to.txt").getChannel();
        //效率高,底层会利用操作系统的 零拷贝进行优化,一次最多只能传输2GB大小数据,大于2GB要循环多次传输
        long size = from.size();
        long capacity = from.size();
        // 分多次传输
        while (capacity > 0) {
            // transferTo返回值为传输了的字节数
            System.out.println("capacity:" + (size - capacity) + " capacity:" + capacity);
            capacity -= from.transferTo(size - capacity, capacity, to);
        }
    }
}
